package com.pearl.generator.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.generator.IDatabaseQuery;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.querys.DbQueryDecorator;
import com.pearl.generator.common.BusinessException;
import com.pearl.generator.common.PageResult;
import com.pearl.generator.pojo.PearlFiledInfo;
import com.pearl.generator.pojo.PearlTableInfo;
import com.pearl.generator.pojo.entity.DataSourceEntity;
import com.pearl.generator.common.Result;
import com.pearl.generator.pojo.mapstruct.DataSourceMapStructMapper;
import com.pearl.generator.pojo.request.DataSourceQuery;
import com.pearl.generator.pojo.request.SaveDataSourceRequest;
import com.pearl.generator.pojo.request.UpdateDataSourceRequest;
import com.pearl.generator.pojo.vo.DataSourceNameVO;
import com.pearl.generator.pojo.vo.DataSourceVO;
import com.pearl.generator.pojo.vo.SimpleTableVO;
import com.pearl.generator.service.IDataSourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author TangDan
 * @since 2022-07-07
 */
@Api(value = "DataSourceController", tags = "数据源管理")
@RestController
@RequestMapping("/dataSource")
public class DataSourceController {

    @Resource
    IDataSourceService dataSourceService;
    @Resource
    DataSourceMapStructMapper dataSourceMapStructMapper;

    @ApiOperation(value = "查询所有数据源", produces = "application/json")
    @GetMapping("/list")
    public Result<?> list() {
        List<DataSourceNameVO> dataSourceNameVOList = dataSourceMapStructMapper.dataSourceEntity2DataSourceNameVO(dataSourceService.list());
        return Result.success(dataSourceNameVOList);
    }

    @ApiOperation(value = "查询表结构详情", produces = "application/json")
    @GetMapping("/tableFiledInfo")
    public Result<List<PearlFiledInfo>> tableInfo(@RequestParam String tableName, @RequestParam Long id) {
        return Result.success(dataSourceService.tableFiledInfo(tableName, id));
    }

    @ApiOperation(value = "查询所有当前数据源下所有表", produces = "application/json")
    @GetMapping("/tableList")
    public Result<List<SimpleTableVO>> tableList(@RequestParam Long id) {
        return Result.success(dataSourceService.tableInfoList(id));
    }


    @ApiOperation(value = "分页查询", produces = "application/json")
    @GetMapping("/page")
    public PageResult<List<DataSourceVO>> page(DataSourceQuery dataSourceQuery) {
        LambdaQueryWrapper<DataSourceEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(StrUtil.isNotBlank(dataSourceQuery.getName()), DataSourceEntity::getName, dataSourceQuery.getName());
        queryWrapper.like(StrUtil.isNotBlank(dataSourceQuery.getUrl()), DataSourceEntity::getUrl, dataSourceQuery.getUrl());
        queryWrapper.like(StrUtil.isNotBlank(dataSourceQuery.getUsername()), DataSourceEntity::getUsername, dataSourceQuery.getUsername());
        queryWrapper.like(StrUtil.isNotBlank(dataSourceQuery.getDbTypeName()), DataSourceEntity::getUrl, dataSourceQuery.getDbTypeName());
        Page<DataSourceEntity> page = dataSourceService.page(new Page<>(dataSourceQuery.getPageNo(), dataSourceQuery.getPageSize()), queryWrapper);
        List<DataSourceEntity> sourceEntityList = page.getRecords();
        List<DataSourceVO> dataSourceVOList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(sourceEntityList)) {
            dataSourceVOList = dataSourceMapStructMapper.dataSourceEntityList2DataSourceVOList(sourceEntityList);
        }
        return PageResult.success(dataSourceVOList, page.getTotal());
    }

    @ApiOperation(value = "新增", produces = "application/json")
    @PostMapping("/save")
    public Result<?> save(@RequestBody @Validated SaveDataSourceRequest saveDataSourceRequest) {
        DataSourceEntity dataSourceEntity = dataSourceMapStructMapper.saveDataSourceRequest2DataSourceEntity(saveDataSourceRequest);
        dataSourceService.save(dataSourceEntity);
        return Result.success();
    }

    @ApiOperation(value = "根据ID删除", produces = "application/json")
    @GetMapping("/remove")
    public Result<?> remove(@RequestParam Long id) {
        dataSourceService.removeById(id);
        return Result.success();
    }

    @ApiOperation(value = "根据ID修改", produces = "application/json")
    @PostMapping("/update")
    public Result<?> update(@RequestBody @Validated UpdateDataSourceRequest updateDataSourceRequest) {
        DataSourceEntity dataSourceEntity = dataSourceMapStructMapper.updateDataSourceRequest2DataSourceEntity(updateDataSourceRequest);
        dataSourceService.updateById(dataSourceEntity);
        return Result.success();
    }

    @ApiOperation(value = "测试连接", produces = "application/json")
    @GetMapping("/testDbOnline")
    Result<Boolean> testDbOnline(String url, String username, String password) {
        return Result.success(dataSourceService.testDbOnline(url, username, password));
    }
}
